package com.LachesismStorm.lachesism.downloader;
import org.apache.http.HttpEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.methods.HttpGet;

import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
 * @Author lachesism
 * @Date 2025-01-06 14:44
 **/
public class sissyHypnoVideoDownloader {
    public static void main(String[] args) {
        try {
            // 传入初始链接
            String initialUrl = "http://example.com"; // 请替换为实际的URL
            List<String> links = getLinksFromPage(initialUrl);

            for (String link : links) {
                // 对每个链接发起请求，获取HTML并解析视频源链接
                String videoSrc = getVideoSourceFromPage(link);
                if (videoSrc != null) {
                    // 获取视频标题并作为文件名
                    String title = getVideoTitle(link);
                    downloadVideo(videoSrc, title);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 获取页面中所有的链接
    public static List<String> getLinksFromPage(String pageUrl) throws IOException {
        List<String> links = new ArrayList<>();
        Document document = Jsoup.connect(pageUrl).get();

        // 提取所有的 <a> 标签
        for (Element link : document.select("a[href]")) {
            String href = link.attr("href");
            if (!href.isEmpty()) {
                links.add(href);
            }
        }
        return links;
    }

    // 从页面中提取视频的源链接
    public static String getVideoSourceFromPage(String pageUrl) throws IOException {
        Document document = Jsoup.connect(pageUrl).get();
        Element videoElement = document.select("#thisPlayer > source").first();
        if (videoElement != null) {
            return videoElement.attr("src");
        }
        return null;
    }

    // 获取视频标题作为文件名
    public static String getVideoTitle(String pageUrl) throws IOException {
        Document document = Jsoup.connect(pageUrl).get();
        Element titleElement = document.select("body > section.page-wrap > section.content-sec > div > div:nth-child(2) > div > div > main > div > div:nth-child(1) > div > div > div:nth-child(3) > div > h1").first();
        if (titleElement != null) {
            return titleElement.text().trim().replaceAll("[^a-zA-Z0-9]", "_");
        }
        return "default_title";
    }

    // 下载视频并保存到本地
    public static void downloadVideo(String videoUrl, String title) throws IOException {
        // 获取视频链接
        URL url = new URL(videoUrl);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");

        // 获取输入流
        InputStream inputStream = connection.getInputStream();
        File file = new File(title + ".mp4");

        // 写入文件
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }

        inputStream.close();
        System.out.println("视频下载完成：" + title + ".mp4");
    }
}
